package com.haier.bims.fridge.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.haier.bims.fridge.bean.FreezeDevice;
import com.haier.bims.fridge.bean.Room;
import com.haier.bims.fridge.service.IFridgeService;
import com.haier.bims.frozenbox.bean.Frozenbox;
import com.haier.common.AbstractController;

@Controller
public class FridgeController extends AbstractController{
	
	private static Logger logger = Logger.getLogger(FridgeController.class);

	private IFridgeService fridgeService;

	public void setFridgeService(IFridgeService fridgeService) {
		this.fridgeService = fridgeService;
	}

	/**
	 * 获取房间和房间内冰箱信息(获取冰箱和所在房间json数据)
	 * 
	 * @return 房间和冰箱信息
	 */
	@RequestMapping(method=RequestMethod.GET, value="/fridge/room")
	@ResponseBody
	public String getRoomInfo(@RequestParam("callback") String callback,@RequestHeader("Accept")String accept) {
		
		List<Room> list = new ArrayList<Room>();
		try {
				list = fridgeService.getRoomInfo();
			} catch (Exception e) {
				logger.error(e.getMessage(), e.getCause());
				//frozenboxs.setReturnCode(1);
			}
		String result = this.pojo2String(list, accept);
		result = callback+result;
		return result;
	}
	
	/**
	 * 获取冰箱信息(根据冰箱id获取冰箱json数据)
	 * @param id
	 * @return 冰箱信息
	 */
	@RequestMapping(method=RequestMethod.GET, value="/fridge/{id}")
	@ResponseBody
	public String getFridgeInfo(@PathVariable String id,@RequestParam("callback") String callback,@RequestHeader("Accept")String accept) {
		
		List<FreezeDevice> devices=fridgeService.qryDisplayDevice(id);
		String result = this.pojo2String(devices, accept);
		result = callback+result;
		return result;
	}
	
	/**
	 * 获取抽屉的盒子信息(根据抽屉id获取盒子json数据)
	 * @param id
	 * @return 盒子信息
	 */
	@RequestMapping(method=RequestMethod.GET, value="/fridge/drawer/{id}")
	@ResponseBody
	public String getDrawerBoxesInfo(@PathVariable String id,@RequestParam("callback") String callback,@RequestHeader("Accept")String accept) {
		
		List<Frozenbox> boxes=fridgeService.qryDrawerBoxes(id);
		String result = this.pojo2String(boxes, accept);
		result = callback+result;
		return result;
	}
}
